import { CustomTableQuestion, InputQuestion, QuestionBase, RadioQuestion, TextQuestion } from '@/components/public-template';
import { TextTable } from '@/components/public-template';
import { TableBase } from '@/components/public-template';
import { Api } from '@/config/api/archives';
import { Api as ApiSystem } from '@/config/api/system';
import { Api as ApiInventory } from '@/config/api/archivesFileInventory';
import { filterTable } from '@/utils/common';
import dayjs from 'dayjs';

class IndexService {
  getQuestions() {
    const questions: QuestionBase<any>[] = [
      new TextQuestion({
        key: 'billCode',
        label: '单据编号',
        value: null
      }),
      new TextQuestion({
        key: 'creator',
        hide: true,
        value: null
      }),
      new TextQuestion({
        key: 'creatorName',
        label: '创建人',
        value: null
      }),
      new TextQuestion({
        key: 'department',
        hide: true,
        value: null
      }),
      new TextQuestion({
        key: 'departmentName',
        label: '创建部门',
        value: null
      }),
      new TextQuestion({
        key: 'createdTime',
        label: '创建时间',
        value: null
      }),
      new TextQuestion({
        key: 'expirationReminder',
        label: '到期提醒时间',
        value: null
      })
    ];
    return questions;
  }
  getReasonQuestions() {
    const questions: QuestionBase<any>[] = [
      new InputQuestion({
        key: 'reason',
        label: '盘点原因',
        required: true,
        prompt: '请输入',
        value: null
      }),
      new InputQuestion({
        key: 'remark',
        label: '备注',
        // type: 'textarea',
        value: null
      })
    ];
    return questions;
  }
  getViewReasonQuestions() {
    const questions: QuestionBase<any>[] = [
      new TextQuestion({
        key: 'reason',
        label: '盘点原因',
        value: null
      }),
      new TextQuestion({
        key: 'remark',
        label: '备注',
        value: null
      })
    ];
    return questions;
  }
  getTaskQuestions() {
    const questions: QuestionBase<any>[] = [
      new CustomTableQuestion({
        key: 'inventoryTasks',
        value: [],
        state: {
          default: {
            inventoryRange: [],
            fileType: [],
            startInventoryTime: null,
            expirationReminder: null,
            inventoryUser: null
          },
          grid: 24,
          fullLine: true, //占整行
          isAdd: true,
          hideBorder: true,
          columns: [
            {
              title: '序号',
              type: 'index',
              key: 'num'
            },
            {
              title: '盘点范围',
              key: 'inventoryRange',
              type: 'tree-select',
              required: true,
              prompt: '请选择',
              width: 300,
              state: {
                url: Api.folderTree,
                treeMultiple: true,
                nameKey: 'name',
                nameValue: 'id',
                nameChildren: 'children',
                protType: 'post'
              }
            },
            {
              title: '档案类型',
              key: 'fileType',
              type: 'tree-select',
              required: true,
              prompt: '请选择',
              width: 300,
              state: {
                url: Api.typeTree,
                treeMultiple: true,
                nameKey: 'name',
                nameValue: 'id',
                nameChildren: 'children',
                protType: 'post'
              }
            },
            {
              title: '盘点开始时间',
              key: 'startInventoryTime',
              type: 'date',
              required: true,
              prompt: '请选择',
              state: {
                disabledDate: (current: any) => {
                  return current && current.isBefore(dayjs().startOf('day'));
                }
              },
              format: 'YYYY-MM-DD'
            },
            {
              title: '盘点结束时间',
              key: 'expirationReminder',
              type: 'date',
              required: true,
              format: 'YYYY-MM-DD',
              state: {
                disabledDate: (current: any) => {
                  return current && current.isBefore(dayjs().startOf('day'));
                }
              },
              prompt: '请选择'
            },
            {
              title: '盘点负责人',
              key: 'inventoryUser',
              type: 'select',
              required: true,
              prompt: '请选择',
              state: {
                url: ApiSystem.subjectListSimple,
                nameKey: 'nickName',
                nameValue: 'id',
                protType: 'post',
                optionFilterProp: 'label'
              }
            },
            {
              title: '操作',
              type: 'operation',
              handle: [
                {
                  type: 'delete',
                  text: '删除'
                }
              ]
            }
          ]
        }
      })
    ];
    return questions;
  }
  getViewTaskQuestions() {
    const questions: QuestionBase<any>[] = [
      new CustomTableQuestion({
        key: 'inventoryTasks',
        value: [
          {
            inventoryRange: [],
            fileType: [],
            startInventoryTime: null,
            expirationReminder: null,
            inventoryUserName: null
          }
        ],
        state: {
          grid: 24,
          fullLine: true, //占整行
          hideBorder: true,
          columns: [
            {
              title: '序号',
              type: 'index',
              key: 'num'
            },
            {
              title: '盘点范围',
              key: 'inventoryRange'
            },
            {
              title: '档案类型',
              key: 'fileType'
            },
            {
              title: '盘点开始时间',
              key: 'startInventoryTime'
            },
            {
              title: '盘点结束时间',
              key: 'expirationReminder'
            },
            {
              title: '盘点负责人',
              key: 'inventoryUserName'
            }
          ]
        }
      })
    ];
    return questions;
  }
  getListQuestions() {
    const questions: QuestionBase<any>[] = [
      new CustomTableQuestion({
        key: 'archivesFileList',
        value: [],
        state: {
          grid: 24,
          fullLine: true, //占整行
          hideBorder: true,
          columns: [
            {
              title: '序号',
              type: 'index',
              key: 'num'
            },
            {
              title: '盘点范围',
              key: 'folderName'
            },
            {
              title: '档案类型',
              key: 'typeName'
            },
            {
              title: '档案盒编码',
              key: 'boxCode'
            },
            {
              title: '档案盒名称',
              key: 'boxName'
            },
            {
              title: '档案编码',
              key: 'filesBoxCode'
            },
            {
              title: '文件名称',
              key: 'name'
            },
            {
              title: '文件编码',
              key: 'code'
            },
            {
              title: '文件号',
              key: 'fileCode'
            },
            {
              title: '文件页数',
              key: 'filePages'
            },
            {
              title: '保管期限',
              key: 'retentionPeriodDate',
              type: 'dictionary',
              state: {
                // 0-永久 10-十年 30-三十年
                filterName: 'DMS_ARCHIVES_FILE_LIST_RETENTION_PERIOD_DATE'
              }
            },
            {
              title: '盘点结果',
              key: 'inventoryResult',
              type: 'select',
              required: true,
              prompt: '请选择',
              tipText: '1、通过：标识数量无误、无破损；\r\n2、不通过：表示存在破损、或者其他异常；',
              icon: 'icon-help-circle',
              options: [
                {
                  label: '通过',
                  value: 1
                },
                {
                  label: '不通过',
                  value: 2
                }
              ]
            },
            {
              title: '说明',
              key: 'resultExplain',
              type: 'input',
              placeholder: '不通过时必填'
            }
          ]
        }
      })
    ];
    return questions;
  }
  getErrorQuestions({ showType }) {
    const columns = [
      {
        title: '序号',
        type: 'index',
        key: 'num'
      },
      {
        title: '盘点范围',
        key: 'folderName'
      },
      {
        title: '档案盒',
        key: 'boxCode'
      },
      {
        title: '档案类型',
        key: 'typeName'
      },
      {
        title: '档案编码',
        key: 'filesBoxCode'
      },
      {
        title: '文件名称',
        key: 'name'
      },
      {
        title: '文件编码',
        key: 'code'
      },
      {
        title: '文件号',
        key: 'fileCode'
      },
      {
        title: '文件页数',
        key: 'filePages'
      },
      {
        title: '保管期限',
        key: 'retentionPeriodDate',
        type: 'dictionary',
        state: {
          filterName: 'DMS_ARCHIVES_FILE_LIST_RETENTION_PERIOD_DATE'
        }
      },
      {
        title: '到期日期',
        key: 'dueDate'
      },
      {
        title: '盘点结果',
        key: 'inventoryResult',
        type: 'dictionary',
        state: {
          filterName: 'PASS_OR_NO'
        },
        colors: {
          2: '#FF4D4F'
        }
      },
      {
        title: '说明',
        key: 'resultExplain'
      },
      showType == 'ERROR'
        ? {
            title: '异常处理',
            key: 'exceptionHandling',
            type: 'select',
            required: true,
            prompt: '请选择',
            options: [
              {
                label: '销毁',
                value: 1
              },
              {
                label: '信息更新',
                value: 2
              }
            ]
          }
        : {
            title: '异常处理',
            key: 'exceptionHandling',
            type: 'dictionary',
            state: {
              filterName: 'EXCEPTION_HANDLER'
            }
          },
      {
        title: '操作',
        type: 'operation',
        handle: [
          {
            type: 'layerFrom',
            text: showType == 'ERROR' ? '更新档案信息' : '预览档案信息',
            showKey: [{ key: 'exceptionHandling', value: 2 }],
            form: showType == 'ERROR' ? this.getUpdateFileQuestions() : this.getViewFileQuestions(),
            params: [{ id: 'id' }],
            config: {
              type: 'layer',
              addUrl: ApiInventory.inventoryUpdate,
              title: showType == 'ERROR' ? '更新档案信息' : '预览更新的档案信息',
              name: 'errorTable',
              cancelSave: showType !== 'ERROR',
              combinationMethod: (data) => {
                console.log('data', data);
                return {
                  listId: data.id,
                  acrCode: data.archivesFileList[0].code,
                  acrName: data.archivesFileList[0].name,
                  boxCode: data.archivesFileList[0].boxCode,
                  dueDate: data.archivesFileList[0].dueDate,
                  filePages: data.archivesFileList[0].filePages,
                  fileTypeName: data.archivesFileList[0].typeName,
                  filesBoxCode: data.archivesFileList[0].filesBoxCode,
                  keyword: data.archivesFileList[0].keyword,
                  summary: data.archivesFileList[0].summary
                };
              }
            }
          }
        ]
      }
    ];
    const questions: QuestionBase<any>[] = [
      new CustomTableQuestion({
        key: 'fileListVoList',
        value: [],
        state: {
          grid: 24,
          fullLine: true, //占整行
          hideBorder: true,
          columns: filterTable(columns, { dueDate: showType == 'ERROR' })
        }
      })
    ];
    return questions;
  }
  getUpdateFileQuestions() {
    const questions: QuestionBase<any>[] = [
      new CustomTableQuestion({
        key: 'archivesFileList',
        value: [],
        state: {
          grid: 24,
          fullLine: true, //占整行
          hideBorder: true,
          columns: [
            {
              title: '序号',
              type: 'index',
              key: 'num'
            },
            {
              title: '文件名称',
              key: 'name'
            },
            {
              title: '文件编码',
              key: 'code'
            },
            {
              title: '档案编码',
              key: 'filesBoxCode'
            },
            {
              title: '档案盒',
              key: 'boxCode'
            },
            {
              title: '档案类型',
              key: 'typeName'
            },
            {
              title: '文件页数',
              key: 'filePages',
              type: 'number'
            },
            {
              title: '到期日期',
              key: 'dueDate',
              type: 'date',
              prompt: '请选择'
            },
            {
              title: '提要',
              key: 'summary',
              type: 'input',
              prompt: '请输入'
            },
            {
              title: '关键词',
              key: 'keyword',
              type: 'input',
              prompt: '请输入'
            }
          ]
        }
      })
    ];
    return questions;
  }
  getViewFileQuestions() {
    const questions: QuestionBase<any>[] = [
      new CustomTableQuestion({
        key: 'archivesFileList',
        value: [],
        state: {
          grid: 24,
          fullLine: true, //占整行
          hideBorder: true,
          columns: [
            {
              title: '文件名称',
              key: 'name'
            },
            {
              title: '文件编码',
              key: 'code'
            },
            {
              title: '文件页数',
              key: 'filePages'
            },
            {
              title: '档案编码',
              key: 'filesBoxCode'
            },
            {
              title: '档案盒',
              key: 'boxCode'
            },
            {
              title: '档案类型',
              key: 'typeName'
            },
            {
              title: '保管期限',
              key: 'retentionPeriodDate',
              type: 'dictionary',
              state: {
                filterName: 'DMS_ARCHIVES_FILE_LIST_RETENTION_PERIOD_DATE'
              }
            },
            {
              title: '提要',
              key: 'summary'
            },
            {
              title: '关键词',
              key: 'keyword'
            }
          ]
        }
      })
    ];
    return questions;
  }
  getTables() {
    const tables: TableBase<any>[] = [
      new TextTable({
        key: 'num',
        value: '序号',
        type: 'index'
      }),
      new TextTable({
        key: 'folderName',
        value: '盘点范围'
      }),
      new TextTable({
        key: 'needInventoryNum',
        value: '应盘数量'
      }),
      new TextTable({
        key: 'actualInventoryNum',
        value: '实盘数量'
      }),
      new TextTable({
        key: 'passNum',
        value: '通过数量'
      }),
      new TextTable({
        key: 'failNum',
        value: '不通过数量'
      }),
      new TextTable({
        key: 'fileCodes',
        type: 'slot',
        state: {
          slotName: 'fileCodes'
        },
        value: '不通过档案'
      })
    ];
    return tables;
  }
  getListTables() {
    const tables: TableBase<any>[] = [
      new TextTable({
        key: 'num',
        value: '序号',
        type: 'index'
      }),
      new TextTable({
        key: 'folderName',
        value: '盘点范围'
      }),
      new TextTable({
        key: 'typeName',
        value: '档案类型'
      }),
      new TextTable({
        key: 'boxCode',
        value: '档案盒编码'
      }),
      new TextTable({
        key: 'boxName',
        value: '档案盒名称'
      }),
      new TextTable({
        key: 'filesBoxCode',
        value: '档案编码'
      }),
      new TextTable({
        key: 'name',
        value: '文件名称'
      }),
      new TextTable({
        key: 'code',
        value: '文件编码'
      }),
      new TextTable({
        key: 'fileCode',
        value: '文件号'
      }),
      new TextTable({
        key: 'filePages',
        value: '文件页数'
      }),
      new TextTable({
        key: 'retentionPeriodDate',
        value: '保管期限',
        type: 'dictionary',
        state: {
          // 0-永久 10-十年 30-三十年
          filterName: 'DMS_ARCHIVES_FILE_LIST_RETENTION_PERIOD_DATE'
        }
      }),
      new TextTable({
        key: 'inventoryResult',
        value: '盘点结果',
        required: true,
        prompt: '请选择',
        tipText: '1、通过：标识数量无误、无破损；\r\n2、不通过：表示存在破损、或者其他异常；',
        icon: 'icon-help-circle',
        controlType: 'select',
        options: [
          {
            label: '通过',
            value: 1
          },
          {
            label: '不通过',
            value: 2
          }
        ]
      }),
      new TextTable({
        key: 'resultExplain',
        value: '说明',
        placeholder: '不通过时必填',
        controlType: 'input'
      })
    ];
    return tables;
  }
  getIcons() {
    const icons: TableBase<any>[] = [
      new TextTable({
        key: 'local',
        value: '详细盘点结果',
        type: 'primary',
        state: {}
      })
    ];
    return icons;
  }
  getCommentQuestions({ showType }) {
    const questions: QuestionBase<any>[] = [
      showType == 'AUDIT'
        ? new RadioQuestion({
            key: 'applyResult',
            label: '意见',
            required: true,
            prompt: '请选择',
            value: 1,
            options: [
              {
                label: '通过',
                value: 1
              },
              {
                label: '驳回',
                value: 2
              }
            ]
          })
        : new RadioQuestion({
            key: 'isReceive',
            label: '意见',
            required: true,
            prompt: '请选择',
            value: true,
            options: [
              {
                label: '通过',
                value: true
              },
              {
                label: '驳回',
                value: false
              }
            ]
          }),
      new InputQuestion({
        key: 'remark',
        label: '备注',
        value: null
      })
    ];
    return questions;
  }
}

export default new IndexService();
